import {request} from '../../utils/http'
import {upload} from '../../utils/upload';

Page({
  data: {
    info:{
      cert_no: '',
      contact_person: '',
      contact_person_mobile: '',
      id_card_back: '',
      id_card_front: '',
      life_check: 0,
      name: '',
      relationship: ''
    },
    relationshipList:['父母','夫妻','兄弟','亲戚','朋友','同事','同学','其他'],
    order_no:'',
		id:'',
  },
  onLoad(query) {
    this.getMemberInfo()
    let card_pay_identity_no_required = my.getStorageSync({key:'card_pay_identity_no_required'}).data

    this.setData({
      id:query.id,
      order_no:query.order_no,
      card_pay_identity_no_required
    })
  },
  getMemberInfo(){
    request('/member/detail', 'get', {
      from:2,
    }).then(res => {
      if (res.code == 200) {
        this.setData({
          info:res.data
        })
      }
    })
  },
  choose(e){
    const type = e.target.dataset.type

    let self = this;
    my.chooseImage({
      sourceType: [ 'album'],
      count: 1,
      success: (res) => {
        console.log(res)
        self.upload(res.apFilePaths[0], type)
      },
      fail: (res) => {
        // 可自行查看错误信息并进行相关处理
        console.log(res);
      }
    })

  },
  upload(path, type){
    upload(path).then(res => {
      console.log(res)
      res = JSON.parse(res)
      if (res.code == 200) {
        let info = this.data.info
        if (type == 'front') {
          info.id_card_front = res.data.url
         
        } else {
          info.id_card_back = res.data.url
          
        }
        this.setData({
          info
        }, () => {
          let card_pay_identity_no_required = my.getStorageSync({key:'card_pay_identity_no_required'}).data

          if (card_pay_identity_no_required != 1 && this.data.info.id_card_front && this.data.info.id_card_back) {
            this.ocrIdCard()
          }
        })
      }
    })
  },
  ocrIdCard(){
    request('/member/ocrIdcard', 'post', {
      id_card_front:this.data.info.id_card_front,
			id_card_back:this.data.info.id_card_back
    }).then(res => {
      if (res.code == 200) {
        let info = this.data.info
        info.cert_no = res.data.number
        info.name = res.data.name
        this.setData({
          info
        })
      } else {
        let info = this.data.info
        info.cert_no = ''
        info.name = ''
        this.setData({
          info
        })
      } 
    }).catch(err => {
      let info = this.data.info
      info.cert_no = ''
      info.name = ''
      this.setData({
        info
      })
    })
  },
  inputPerson(e){
      let info = this.data.info
      info.contact_person = e.detail.value
      this.setData({
        info
      })
  },
  inputMobile(e){
    let info = this.data.info
    info.contact_person_mobile = e.detail.value
    this.setData({
      info
    })
  },
  relationShipChange(e){
    let info = this.data.info
    info.relationship = this.data.relationshipList[e.detail.value]
    this.setData({
      info
    })
  },
  submit(){
    let form = JSON.parse(JSON.stringify(this.data.info))
    form['from'] = 2
    // form['order_no'] = this.order_no
    form['order_id'] = this.data.id

    request('/member/update', 'post', form).then(res => {
      if (res.code == 200) {
        if(res.data.contract_signurl) {
          my.setStorageSync({
            data: form.order_id,
            key: 'sign_order_id'
          });

          my.setStorageSync({
            data: 3,
            key: 'sign_order_type'
          });
          my.navigateTo({
            url:`/pages/sign-webview/sign-webview?url=${res.data.contract_signurl}`
          })
        } else {
          this.startAPVerify({
            url:res.data.life_check_h5,
            certifyId:res.data.certify_id
          }, (verifyResult) => {
            if (verifyResult.resultStatus === '9000') {
              my.navigateTo({
                url:`/pages/bind-bank/bind-bank?id=${form.order_id}&type=1`
              })
            }
          })
        }
        
      }
    })
  },
  startAPVerify(options, callback){
    my.call('startBizService', {
      name:'open-certify',
      param: JSON.stringify(options),
    }, callback)
  }
});
